package com.abo.yaas.view.widgets.fields;

import com.abo.yaas.domain.Language;
import com.abo.yaas.i18n.Lang;
import com.abo.yaas.i18n.LocaleChangeListener;
import com.abo.yaas.i18n.LocaleChangedEvent;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.ui.ComboBox;

@SuppressWarnings("serial")
public class LocaleCombo extends ComboBox implements LocaleChangeListener {
    final BeanContainer<String, Language> container = new BeanContainer<String, Language>(Language.class);

    public LocaleCombo() {
        super();
        setSizeUndefined();
        setContainerDataSource(container);
        setImmediate(true);
        setNullSelectionAllowed(false);
        setWidth("80px");
        container.addItem("en", new Language(null, "English"));
        container.addItem("ru", new Language(null, "Русский"));
        setItemCaptionPropertyId("name");
    }

    @Override
    public void attach() {
        super.attach();
        Lang.addListener(this);
    }
    
    @Override
    public void detach() {
        Lang.removeListener(this);
        super.detach();
    }
    
    @Override
    public void localeChanged(LocaleChangedEvent event) {
        setValue(event.getLocale().getLanguage());
    }
}
